package engine.systems.motion.rotateToRadius
{
	import engine.classes.enumerations.SystemPriority;
	import engine.groups.Group;
	import engine.systems.spatial.spatialOffset.SpatialOffsetData;
	import engine.systems.update.GameSystem;
	
	
	public class RotateToRadiusSystem extends GameSystem
	{
		private const ROTATE_TO_RADIUS:String = "rotate_to_radius";
		
		private var data:SpatialOffsetData;
		
		public function RotateToRadiusSystem()
		{
			super(RotateToRadiusNode, updateNode, nodeAdded, nodeRemoved);
			
			this.priority = SystemPriority.PRE_MOTION;
		}
		
		private function updateNode(node:RotateToRadiusNode, time:Number):void
		{
			node.rotate.radians += node.rotate.rate * time;
			
			data = node.offset.get(ROTATE_TO_RADIUS);
			
			data.x = Math.cos(node.rotate.radians) * node.rotate.radius;
			data.y = Math.sin(node.rotate.radians) * node.rotate.radius;
		}
		
		private function nodeAdded(node:RotateToRadiusNode):void
		{
			node.offset.add(ROTATE_TO_RADIUS, new SpatialOffsetData());
		}
		
		private function nodeRemoved(node:RotateToRadiusNode):void
		{
			node.offset.remove(ROTATE_TO_RADIUS);
		}
	}
}